package cn.edu.shou.lab.config.utils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class PackageUtil {
    private static Logger logger = Logger.getLogger(PackageUtil.class.getName());
//    public static void main(String[] args) {
//        String packageName = SHOU.ENTITY_PATH;
//        List<String> classNames = getClassName(packageName);
//        for (String className : classNames) {
//            System.out.println(className);
//        }
//    }

    public static List<String> getClassName(String packageName) {
        try {
            String filePath = ClassLoader.getSystemResource("").getPath() + packageName.replace(".", "\\");
            filePath = URLDecoder.decode(filePath,"utf-8");
            return getClassName(filePath, null);
        } catch (UnsupportedEncodingException e) {
            logger.warning(e.getMessage());
        }
        return null;
    }

    private static List<String> getClassName(String filePath, List<String> className) {
        List<String> myClassName = new ArrayList<>();
        File file = new File(filePath);
        for (File childFile : file.listFiles()) {
            if (childFile.isDirectory()) {
                myClassName.addAll(getClassName(childFile.getPath(), myClassName));
            } else {
                String childFilePath = childFile.getPath();
                childFilePath = childFilePath.substring(childFilePath.indexOf("\\classes") + 9, childFilePath.lastIndexOf("."));
                childFilePath = childFilePath.replace("\\", ".");
                myClassName.add(childFilePath);
            }
        }
        return myClassName;
    }
}